package com.woniuxy.bookmall.service;

import com.woniuxy.bookmall.entity.TItem;
import com.woniuxy.bookmall.entity.TUser;
import com.woniuxy.bookmall.mapper.TBookMapper;
import com.woniuxy.bookmall.mapper.TItemMapper;
import com.woniuxy.bookmall.utils.RedisUtils;
import com.woniuxy.bookmall.vo.Cart;
import com.woniuxy.bookmall.vo.EsBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class CartService {

    @Autowired
    RedisUtils redisUtils ;
    @Autowired
    TBookMapper  bookMapper ;

    public void addCart(Long bookId, int buyCount, TUser user){
        EsBook esBook = bookMapper.selectEsBookById(bookId);
        Cart cart = new Cart();
        cart.setBcount(buyCount);
        cart.setBookid(bookId);
        cart.setBookname(esBook.getName());
        cart.setPrice(esBook.getPrice());
        cart.setImgsrc(esBook.getImgsrc());
        BigDecimal price = esBook.getPrice();
        cart.setSumprice(price.multiply(new BigDecimal(buyCount)));
        // 1.判断购物车中 有没有当前 商品
            // 1.1 有  ，更改购买数量
            //1.2  没有 ， 添加一条薪的数据
        //存储购物车数据时 ，利用redis 的hash数据类型
        String key = "WONIU"+user.getId();
        boolean flag = redisUtils.hasKey(key);
        if (!flag) {
            //当前用户没有购物车
            // 封装一个 Item
            redisUtils.hset(key,bookId+"",cart);
        }else {
            //判断当前商品是否存在
            boolean b = redisUtils.hHasKey(key, bookId + "");
            if (b) {
                //已经购买过
                Cart oldCart = (Cart) redisUtils.hget(key,bookId+"");
                cart.setBcount(oldCart.getBcount()+buyCount);
                cart.setSumprice(esBook.getPrice().multiply(new BigDecimal(cart.getBcount())));
                // 2 x 3

                redisUtils.hset(key,bookId+"",cart);
            }else {
                //没有购买过
                redisUtils.hset(key,bookId+"",cart);
            }
        }
    }

    /**
     * 修改购车中的商品数量
     * @param bookId
     * @param buyCount
     * @param user
     */
    public void modifyCart(Long bookId, int buyCount, TUser user) {
        // 从购物车中查询出 当前Item
        String key = "WONIU"+user.getId();
        Cart item = (Cart) redisUtils.hget(key,bookId+"");
        item.setBcount(buyCount);
        item.setSumprice(item.getPrice().multiply(new BigDecimal(buyCount)));

        //重新添加
        redisUtils.hset(key,bookId+"",item);
    }

    /**
     * 删除 购物车中的商品
     * @param bookId
     * @param user
     */
    public void delCart(Long bookId, TUser user) {
        String key = "WONIU"+user.getId();
        redisUtils.hdel(key,bookId+"");
    }

    /**
     * 批量删除
     * @param bookIds
     * @param user
     */
    public void delBatchCart(String[] bookIds, TUser user) {
        String key = "WONIU"+user.getId();
        redisUtils.hdel(key, bookIds);
    }
    public Collection<Object> findCartByUser(TUser user) {
        String key = "WONIU"+user.getId();
        Map<Object, Object> entries = redisUtils.hmget(key);
        Collection<Object> values = entries.values();
        return values ;
    }

    public void clearCartuser(TUser user) {
        String key = "WONIU"+user.getId();
        redisUtils.del(key);
    }

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    public Collection<Object> findCartByCal(TUser user, String[] bids) {
        String key = "WONIU"+user.getId();
        List<Object> objects = redisTemplate.opsForHash().multiGet(key,Arrays.asList(bids));
        return objects ;
    }
}
